#!/usr/bin/env python
# -*- coding:UTF-8 -*-
__author__ = 'wycao'

from selenium import webdriver
import json,time
from jiebatitle.jbtitle import JbTitle
from pgre.postgkeyword import PostKeyword


class SelmPdf:
    def __init__(self):
        self.dr = webdriver.PhantomJS()
        self.jb = JbTitle()
        self.pk = PostKeyword()
        self.category_dict = {"category_ndbg_szsh": "年度报告",
                         "category_bndbg_szsh": "半年度报告",
                         "category_yjdbg_szsh": "一季度报告",
                         "category_sjdbg_szsh": "三季度报告"}
        self.catevalues = self.category_dict.values()


    def get_title(self):
        year = '年'
        startid = 1205155757
        try:
            for i in range(10000000):
                url = r'http://www.cninfo.com.cn/cninfo-new/disclosure/szse/bulletin_detail/true/'+str(startid)
                self.dr.get(url=url)
                try:
                    filetitle = self.dr.find_element_by_xpath("html/body/div[1]/div[1]/div[1]/h2").text
                    filetitle_list = filetitle.split("\n")
                    title = filetitle_list[1]
                    title_list = self.jb.cut_text(filetitle)
                    try:
                        for ca in self.catevalues:
                            if ca in title_list:
                                print(title)
                                values = []
                                # caindex = title_list.index(ca)
                                code = title_list[0]
                                yearindex = title_list.index(year)
                                portdute = "".join(title_list[yearindex-1:yearindex+1])
                                cmpname = "".join(title_list[1:yearindex-1])
                                portstatus = title_list[-1]
                                values.append(code)
                                values.append(cmpname)
                                values.append(title)
                                values.append(url)
                                values.append(portdute)
                                values.append(ca)
                                values.append(portstatus)
                                self.pk.insert_another_date(values)
                    except BaseException as e:
                        print(e)
                except BaseException as e:
                    print(e)
                startid -= 1
                print(startid)
        except BaseException as e:
            print(e)
        finally:
            self.pk.conn.close()
            self.dr.close()

sp = SelmPdf()
sp.get_title()
# sp.dr.close()